package cn.zhz.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import cn.zhz.service.provaider.ProviderService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

import cn.zhz.pojo.Bill;
import cn.zhz.pojo.Provider;
import cn.zhz.pojo.User;
import cn.zhz.service.bill.billService;

import cn.zhz.tools.Constants;
import cn.zhz.tools.PageSupport;
@Controller
@RequestMapping("/bill")
public class BillController {
    private Logger logger=Logger.getLogger(BillController.class);
    @Resource
    private billService billservice;
    @Resource
    private ProviderService providerService;
    @RequestMapping(value = "/BillList")
    public String goBillList(Model model,
                             @RequestParam(value="queryProductName",required=false)String queryProductName
            ,@RequestParam(value="queryProviderId",required=false)String queryProviderId
            ,@RequestParam(value="queryIsPayment",required=false)String queryIsPayment
            ,@RequestParam(value="pageIndex",required=false)String pageIndex) {
        int _queryProviderId=0;  //供应商id
        int _queryIsPayment=0;	//是否支付
        List<Bill>billList=null;
        List<Provider>providerList=null;
        //设置当前页面
        int pageSize=Constants.pageSize;
        //当前页面
        int currentPageNo = 1;
        if (pageIndex != null && !pageIndex.equals("")) {
            currentPageNo = Integer.parseInt(pageIndex);
        }
        //查询名称
        if (queryProductName == null) {
            queryProductName = "";
        }
        //查询供应商id
        if (queryProviderId != null && !queryProviderId.equals("")) {
            _queryProviderId=Integer.parseInt(queryProviderId);
        }
        //查询是否支付
        if (queryIsPayment != null && !queryIsPayment.equals("")) {
            _queryIsPayment=Integer.parseInt(queryIsPayment);
        }
        //总数量表
        int totalCount=billservice.getBillCount(queryProductName, _queryProviderId, _queryIsPayment);
        //总页数
        PageSupport pages=new PageSupport();
        pages.setPageSize(pageSize); //每页显示的数量
        pages.setTotalCount(totalCount);//总数量
        //控制首尾页
        if (currentPageNo<1) {
            currentPageNo=1;
        }else if (currentPageNo>pages.getTotalPageCount()) {
            currentPageNo=pages.getTotalPageCount();
        }
        pages.setCurrentPageNo(currentPageNo); //设置当前页码
        billList=billservice.getBillList(queryProductName, _queryProviderId, _queryIsPayment, currentPageNo, pageSize);
        providerList=providerService.getAllProvider();
        model.addAttribute("billList",billList);
        model.addAttribute("providerList",providerList);
        model.addAttribute("pages",pages);
        model.addAttribute("queryProductName",queryProductName);
        model.addAttribute("queryProviderId",_queryProviderId);
        model.addAttribute("queryIsPayment",_queryIsPayment);
        return "billlist";
    }
    /**
     * 订单详细
     * @param billid
     * @param model
     * @return
     */
    @RequestMapping(value="/getBillById")
    public String getBillById(@RequestParam(value="billid")String billid,Model model) {
        logger.info("getBillById=======================>");
        Bill bill =billservice.getBillById(Integer.parseInt(billid));
        model.addAttribute("bill",bill);
        return "billview";
    }
    /**
     * 跳转bill修改页面
     * @param bill
     * @param model
     * @param billid
     * @return
     */
    @RequestMapping(value="/UpdateBill")
    public String goUpdateBill(@ModelAttribute("bill") Bill bill,Model model,@RequestParam(value="billid")String billid) {
        Bill _bill =billservice.getBillById(Integer.parseInt(billid));
        model.addAttribute("bill",_bill);
        return "billmodify";
    }
    /**
     * 更新订单信息
     * @param bill
     * @param session
     * @return
     */
    @RequestMapping(value="/updateBill")
    public String updateBill(Bill bill,HttpSession session) {
        bill.setModifyDate(new Date());
        bill.setModifyBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
        if (billservice.modify(bill)>0) {
            return "redirect:/bill/BillList";
        }else {
            return "billmodify";
        }
    }
    /**
     * 异步验证删除订单
     * @param billid
     * @return
     */
    @RequestMapping(value="/delBill",method=RequestMethod.GET)
    @ResponseBody
    public Object delBill(@RequestParam("billid")int billid) {
        HashMap<String,Object>map=new HashMap<String, Object>();
        logger.info("bill===>delBill===============================");
        Bill bill=billservice.getBillById(billid);
        if (bill!=null) {
            if (billservice.deleteBillById(billid)>0) {
                map.put("delResult", "true");
            }else {
                map.put("delResult", "false");
            }
        }else {
            map.put("delResult", "notexist");
        }
        return JSONArray.toJSONString(map);
    }
    /**
     * 跳转新增bill页面
     * @param bill
     * @return
     */
    @RequestMapping(value="/goaddBill")
    public String goAddBill(@ModelAttribute("bill") Bill bill) {
        return "billadd";
    }
    /**
     * 新增订单
     * @param bill
     * @param session
     * @return
     */
    @RequestMapping(value="/addBill")
    public String addBill(Bill bill,HttpSession session) {
        bill.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
        bill.setCreationDate(new Date());
        if (billservice.add(bill)>0) {
            return "redirect:/bill/BillList";
        }else {
            return "billadd";
        }
    }
}